import 'package:app/components/TimeBox.dart';
import 'package:app/model/app_config.dart';
import 'package:app/model/model.dart';
import 'package:app/page/add.dart';
import 'package:app/page/countdownView.dart';
import 'package:app/router/router.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class ReadBookPage extends StatefulWidget
    with RouterBridge<CountdownEventClass> {
  const ReadBookPage({super.key});
  @override
  State<ReadBookPage> createState() => _ReadBookPageState();
}

class _ReadBookPageState extends State<ReadBookPage> {
  var isInit = false;
  late final CountdownEventClass countdownEventClass;
  final cList = CountdownEventClassList.create();
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (!isInit) {
      final args = widget.argumentsOf(context);
      if (args != null) {
        countdownEventClass = args;
        isInit = true;
      }
    }
  }

  Widget _iconEmpty(BuildContext context) {
    return const TDEmpty(type: TDEmptyType.plain, emptyText: '事件为空');
  }

  void swipeLeftEdit(BuildContext context, ChildrenEvent event) {
    context.routeOf<Add>().arguments(event).to();
  }

  void swipeLeftDelete(BuildContext context, ChildrenEvent event) {
    showGeneralDialog(
      context: context,
      pageBuilder: (
        BuildContext buildContext,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
      ) {
        return TDAlertDialog(
          content: '确定删除${event.name.value}吗？',
          rightBtnAction: () {
            var countdownEvent = cList.findCountdownEventClassById(
              event.parentId.value,
            );
            if (countdownEvent == null) {
              TDToast.showFail("参数错误", context: context);
              return;
            }
            countdownEvent.deleteEventChild(event.childrenId.value);
            Navigator.of(buildContext).pop();
          },
          leftBtnAction: () {
            Navigator.of(buildContext).pop();
          },
        );
      },
    );
  }

  void toCountdownView(ChildrenEvent args) {
    context.routeOf<CountdownView>().arguments(args).to();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TDNavBar(
        height: 48,
        title: countdownEventClass.name.value,
        titleColor: Colors.white,
        centerTitle: false,
        backgroundColor: StaticAppConfig.appBarColor,
        titleFontWeight: FontWeight.w600,
        useDefaultBack: false,
        screenAdaptation: true,
        leftBarItems: [
          TDNavBarItem(
            icon: Icons.chevron_left,
            iconColor: Colors.white,
            action: () {
              Navigator.maybePop(context);
            },
          ),
        ],
        rightBarItems: [
          TDNavBarItem(
            icon: TDIcons.table_add,
            iconSize: 24,
            iconColor: Colors.white,
            action: () {
              Map<String, dynamic> dartMap = {
                "childrenId": "",
                "name": "",
                "parentId": countdownEventClass.id.value,
                "targetTimestamp": DateTime.now().millisecondsSinceEpoch,
                "isprecise": false,
                "whetherToTop": false,
                "eventColor":StaticAppConfig.defaultEventColor.toARGB32()
              };
              context
                  .routeOf<Add>()
                  .arguments(ChildrenEvent.fromJson(dartMap))
                  .to();
            },
          ),
        ],
      ),
      body: Container(
        color: Color(0xFFCCCCCC),
        padding: EdgeInsets.symmetric(horizontal: 10),
        child: ListView.builder(
          itemCount: countdownEventClass.eventChildren.length,
          itemBuilder: (context, index) {
            return Obx(() {
              if (countdownEventClass.eventChildren.isEmpty) {
                return Expanded(
                  flex: 1,
                  child: Container(
                    alignment: Alignment.center,
                    child: _iconEmpty(context),
                  ),
                );
              }
              var event = countdownEventClass.eventChildren[index];
              return Column(
                children: [
                  if(index==0)SizedBox(height: 10),
                  TimeBox(
                    name: event.name.value,
                    targetTimestamp: event.targetTimestamp.value,
                    children: [
                      TDSwipeCellAction(
                        flex: 20,
                        backgroundColor: Color(0xFFFA9550),
                        label: '编辑',
                        onPressed: (context) {
                          swipeLeftEdit(context, event);
                        },
                      ),
                      TDSwipeCellAction(
                        flex: 20,
                        backgroundColor: Color(0xFFD54941),
                        label: '删除',
                        onPressed: (context) {
                          swipeLeftDelete(context, event);
                        },
                      ),
                    ],
                    onTap: () {
                      toCountdownView(event);
                    },
                  ),
                  if (index < countdownEventClass.eventChildren.length - 1)
                    SizedBox(height: 10),
                ],
              );
            });
          },
        ),
      ),
    );
  }
}
